package com.yowasa.xiuxian.engine.battle.strategy.skill.wood;

import com.google.common.collect.Lists;
import com.yowasa.xiuxian.annoations.BS;
import com.yowasa.xiuxian.constant.XiuxianConstants;
import com.yowasa.xiuxian.engine.battle.strategy.CalAbility;
import com.yowasa.xiuxian.entity.bo.battle.BattleContent;
import com.yowasa.xiuxian.entity.bo.battle.UserBattleContent;
import com.yowasa.xiuxian.engine.battle.strategy.InherentStrategy;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.ElementEnum;
import com.yowasa.xiuxian.entity.enums.InherentTiming;
import com.yowasa.xiuxian.exception.HandlerException;
import org.springframework.core.annotation.Order;

import java.math.BigDecimal;
import java.util.List;

@BS(name="长生术")
@Order(-1000)
public class ChangShengShu extends CalAbility implements InherentStrategy {
    @Override
    public ElementEnum getElement() {
        return ElementEnum.WOOD;
    }

    @Override
    public void action(UserBattleContent first, UserBattleContent second, BattleContent content) {
        BigDecimal divideCmp = new BigDecimal(first.getBattleInfo().getCmp()).multiply(new BigDecimal(first.getBattleInfo().getRestoreRate())).divide(new BigDecimal(100),0);
        int cmp = divideCmp.intValue();
        BigDecimal divideSpell = new BigDecimal(first.getBattleInfo().getHp() / 20).multiply(new BigDecimal(first.getBattleInfo().getRestoreRate())).divide(new BigDecimal(100),0);
        int spell = divideSpell.intValue();
        int needHp = first.getBattleInfo().getHp() - first.getBattleInfo().getChp();
        List<Integer> nums = Lists.newArrayList(cmp, spell, needHp);
        nums.sort(Integer::compareTo);
        Integer can = nums.get(0);
        int cost=new BigDecimal(can).multiply(new BigDecimal(100)).divide(new BigDecimal(first.getBattleInfo().getRestoreRate()),0).intValue();
        first.getBattleInfo().setChp(first.getBattleInfo().getChp() + can);
        first.getBattleInfo().setCmp(first.getBattleInfo().getCmp() - cost);
        content.getMsgLog().add(first.getUserBO().getName() + "因长生术回复" + can + "点血量");
    }

    @Override
    public void limit(UserBO user) {
        if (user.getElement().get("木") < XiuxianConstants.DEFAULT_ELEMENT_ACTIVITY) {
            throw new HandlerException("学习/使用长生术必须具有木灵根");
        }
    }
    @Override
    public String limitDesc() {
        return "具有木灵根";
    }

    @Override
    public boolean check(UserBattleContent user, BattleContent content) {
        if (user.getBattleInfo().getChp() >= user.getBattleInfo().getHp()) {
            return false;
        }
        if (user.getBattleInfo().getCmp() <= 0) {
            return false;
        }
        return true;
    }

    @Override
    public String desc() {
        return "自己行动开始时消耗等量mp最高回复5%最大生命值的血量";
    }

    @Override
    public List<InherentTiming> getTiming() {
        return Lists.newArrayList(InherentTiming.SELF_BEGIN);
    }


}
